/*
**  (C) by Rael Delcon (rael.delcon@gmail.com)
**
** Permission to use, copy, modify and distribute this code and
** its documentation for any purpose is hereby granted without
** fee, provided that the above copyright notice, or equivalent
** attribution acknowledgement, appears in all copies and
** supporting documentation.
**
** Copyright holder makes no representations about the suitability
** of this software for any purpose. It is provided "as is" without
** express or implied warranty.
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>

#define m3Dfirst   0xFFFF
#define m3Dall     0xFFFF
#define m3Dnil     0xFFFF
#define m3Dmax     0xFFFE
#define m3DnoCoord 1E11

typedef unsigned short m3Dindex;

typedef struct {
  float x;
  float y;
  float z;
  m3Dindex f;
  short mrk;
} m3Dpoint;

typedef struct {
  m3Dindex verts[3];
  m3Dindex next[3];
  short mrk;
  short inf;
} m3Dface;

typedef struct {
  m3Dpoint *verts;
  m3Dface  *faces;
  m3Dindex  vnum;
  m3Dindex  vsize;
  m3Dindex  fnum;
  m3Dindex  fsize;
} m3Dmesh;


m3Dmesh *m3Dnew();
m3Dmesh *m3Dfree(m3Dmesh *m);

m3Dmesh *m3DloadObj(FILE *f);
int m3DsaveObj(FILE *fout, m3Dmesh *m);
int m3DsaveWtri(FILE *fout, m3Dmesh *m);
int m3DsaveRaw(FILE *fout, m3Dmesh *m);

m3Dindex m3DaddVert(m3Dmesh *m, float x, float y, float z);
m3Dindex m3DaddFace(m3Dmesh *m, m3Dindex v0, m3Dindex v1, m3Dindex v2);

m3Dindex m3DnextFaceOn(m3Dmesh *m, m3Dindex v, m3Dindex cur);
m3Dindex m3DadjFace(m3Dmesh *m, m3Dindex f, m3Dindex side);

void m3DmarkFace(m3Dmesh *m, m3Dindex f, short mrk);
void m3DmarkVert(m3Dmesh *m, m3Dindex v, short mrk);

int m3DnumEdges(m3Dmesh *m);
#define m3DnumFaces(m) ((m)->fnum)
#define m3DnumVerts(m) ((m)->vnum)

